package com.pkche.core.advanced.interceptor;

import java.lang.reflect.Method;

import javax.servlet.http.HttpSession;

import com.pkche.core.advanced.model.Deleted;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.pkche.core.advanced.model.BaseModel;

/**
 * 保存数据之前自动填充实体的founderid、createdate、deletedstate属性的值
 *
 */
public class BaseModelBeforeSaveAdvice implements MethodBeforeAdvice {
	@Override
	public void before(Method method, Object[] args, Object target)
			throws Throwable {
		Object obj = args[0];
		if(BaseModel.class.isAssignableFrom(obj.getClass())){
			BaseModel model = (BaseModel) obj;
			ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
			HttpSession session = attr.getRequest().getSession();
		//	Long loginUserId = (Long) session.getAttribute("login_user_id");			
		//	model.setFounderid(loginUserId);
		//	model.setCreateDate(new Timestamp(System.currentTimeMillis()));
		}
		if(Deleted.class.isAssignableFrom(obj.getClass())){
			Deleted deletedModel = (Deleted) obj;
			deletedModel.setDeletedState(0);
		}
	}

	

}
